package hotel.quartz.oversea.job.task;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.BatchPushRoomDatasVo;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Component
public class SwitchSaleRoomTask {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    private String timestamp;

    private void setTimestamp(String timestamp){
        this.timestamp = timestamp;
    }

    /**
     * 调用 携程 价格修改接口
     * @param switchPriceChangeUrl
     * @param timestamp
     * @throws Exception
     */
    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void execute(String switchPriceChangeUrl, BatchPushRoomDatasVo batchPushRoomDatasVo, String timestamp){
        setTimestamp(timestamp);

        log.info("Meituan Hotel Goods Price Change Query Job {} >>>>>>>>> 异步线程 主动查询产品-调用携程批量修改价格 SwitchSaleRoomTask.execute 开始执行时间：{}",timestamp, DateUtil.getNowTime());
        //Map params = new HashMap();
        //params.put("batchPushRoomDatasVo",batchPushRoomDatasVo);
        //发起请求
        Result response = JobRestTemplateUtils.post(switchPriceChangeUrl, JSON.toJSONString(batchPushRoomDatasVo),restTemplate);
        log.info("Meituan Hotel Goods Price Change Query Job {} >>>>>>>>> execute 异步线程 主动查询产品-调用携程批量修改价格 返回: {},{},{}", timestamp, JSON.toJSONString(batchPushRoomDatasVo), hotel.base.oversea.utils.DateUtil.getCurrTime(),response, JSON.toJSONString(batchPushRoomDatasVo));
        if(response.getCode() == CommonConstants.SUCCESS){

        }else{
            //还原
            //redisTemplate.opsForList().leftPush(MeituanConstants.REIDS_POP_ALL_SALE_ROOM_ABOUT_MT_HOTEL_IDS,String.valueOf(batchPushRoomDatasVo.getMtHotelId()));
            //log.info("Meituan Hotel Goods Price Change Query Job {} 异步线程 SwitchSaleRoomTask.execute 请求失败：{}",timestamp,response);

            if(!ObjectUtils.isEmpty(response) && !response.getMsg().toString().contains("部分数据没有推送成功")){
               // SendUtils.dingMsg("主动查询价格房态定时器-推送携程失败-请关注！",DateUtil.getCurrTime()+" SwitchSaleRoomTask url="+switchPriceChangeUrl+" | response="+JSON.toJSONString(response)+" | paramsJson过长请根据timestamp="+timestamp+",查询日志 ",MessageEnums.genre.ROOM_CHANGE_FOCUS.getCode(),restTemplate);
            }
        }

        log.info("Meituan Hotel Goods Price Change Query Job {} >>>>>>>>> 异步线程 主动查询产品-调用携程批量修改价格 SwitchSaleRoomTask.execute 结束执行时间：{}",timestamp, DateUtil.getNowTime());
    }
   /* @Async
    public Future<String> doTaskTwo() throws Exception {
        System.out.println("task2");
        return new AsyncResult<>("任务一完成");
    }*/
   public static void main(String[] args) {
       BatchPushRoomDatasVo batchPushRoomDatasVo = new BatchPushRoomDatasVo();
       batchPushRoomDatasVo.setMtHotelId(11);
       System.out.printf("==="+ JSON.toJSONString(batchPushRoomDatasVo));

   }
}
